package com.xl.rentkeeper.business;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;

import com.xl.fuse.protos.AllRetCode;
import com.xl.fuse.protos.msg.Msg;
import com.xl.fuse.protos.packet.Packet;
import com.xl.fuse.protos.viewroom.AddReserveRoomReq;
import com.xl.fuse.protos.viewroom.AddReserveRoomResp;
import com.xl.fuse.protos.viewroom.AddViewReq;
import com.xl.fuse.protos.viewroom.AddViewResp;
import com.xl.fuse.protos.viewroom.ConfirmViewReq;
import com.xl.fuse.protos.viewroom.ConfirmViewResp;
import com.xl.fuse.protos.viewroom.QueryOrderReq;
import com.xl.fuse.protos.viewroom.QueryOrderResp;
import com.xl.fuse.protos.viewroom.ReserveRoomReq;
import com.xl.fuse.protos.viewroom.ReserveRoomResp;
import com.xl.fuse.protos.viewroom.RoomFilter;
import com.xl.fuse.protos.viewroom.RoomInfo;
import com.xl.fuse.protos.viewroom.RoomSerachReq;
import com.xl.fuse.protos.viewroom.RoomSerachResp;
import com.xl.fuse.protos.viewroom.SignInfo;
import com.xl.fuse.protos.viewroom.StewardOrderReq;
import com.xl.fuse.protos.viewroom.StewardOrderResp;
import com.xl.fuse.protos.viewroom.ViewOrder;
import com.xl.fuse.protos.viewroom.ViewOrderStatus;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.act.MainActivity;
import com.xl.rentkeeper.db.orm.ORMap;
import com.xl.rentkeeper.entity.Order;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.mgr.ThreadManager;
import com.xl.rentkeeper.mgr.UiObserverManager;
import com.xl.rentkeeper.net.Request;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by zwc on 2015/7/24.
 */
public class OrderLogic extends BaseLogic {
    public static int pageSize = 10;
    public static String CMD_ORDER_NOT_RECEIVE_AND_CMD_ORDER_IS_RECEIVE = "ORDER_NOT_RECEIVE|ORDER_IS_RECEIVE";//待接单、已接单，未确认
    public static String CMD_ORDER_IS_VIEW = "ORDER_IS_VIEW";//已联系
    public static String CMD_ORDER_IS_SIGN = "ORDER_IS_SIGN";//已签约
    public static String CMD_ORDER_NOT_SIGN = "ORDER_NOT_SIGN";//未签约&已看
    public static String CMD_ORDER_NOT_VIEW = "ORDER_NOT_VIEW";//不看房
    public static String CMD_PROCESSING_ORDER_STATUS_UPDATE = "PROCESSING_ORDER_STATUS_UPDATE";//正在处理订单状态发生变化的时候
    public static Map<String, String> statusMap = null;

    static {
        statusMap = new HashMap<>();
        statusMap.put(CMD_ORDER_NOT_RECEIVE_AND_CMD_ORDER_IS_RECEIVE, String.valueOf(ViewOrderStatus.ORDER_NOT_RECEIVE.getValue()) + "," + String.valueOf(ViewOrderStatus.ORDER_IS_RECEIVE.getValue()));
        statusMap.put(CMD_ORDER_IS_VIEW, String.valueOf(ViewOrderStatus.ORDER_IS_VIEW.getValue()));
        statusMap.put(CMD_ORDER_IS_SIGN, String.valueOf(ViewOrderStatus.ORDER_IS_SIGN.getValue()));
        statusMap.put(CMD_ORDER_NOT_SIGN, String.valueOf(ViewOrderStatus.ORDER_NOT_SIGN.getValue()));
        statusMap.put(CMD_ORDER_NOT_VIEW, String.valueOf(ViewOrderStatus.ORDER_NOT_VIEW.getValue()));
    }


    private Map<Integer,Order> orderIdMap = new ConcurrentHashMap<>();// order id -> order
    private Map<Integer,List<Order>> orderStatusMap = new ConcurrentHashMap<>();// status -> list<Order> desc by update timestamp

    public static OrderLogic getInstance() {
        return (OrderLogic) SingletonRegistry.getInstance(OrderLogic.class);
    }


    private OrderLogic() {
        loadFromDb();
    }

    private void loadFromDb() {
        ThreadManager.getInstance().runOnWorkerThread(new Runnable() {
            @Override
            public void run() {
                List<Order> orders = getDb().find(ORMap.ORDER_PROCESSING, Order.class, null, null, null, null, null);
                lock.lock();
                onRecieveViewOrders(orders, true);
                lock.unlock();

            }
        }, 0);
        ThreadManager.getInstance().runOnWorkerThread(new Runnable() {
            @Override
            public void run() {
                List<Order> orders = getDb().find(ORMap.ORDER_HISTORY, Order.class, null, null, null, null, null);
                lock.lock();
                onRecieveViewOrders(orders, true);
                lock.unlock();

            }
        }, 0);
    }

    //按照订单状态查询订单
    public void queryOrder(int order_id, String status, long minUpdateTime, long maxUpdateTime) {
        int size = pageSize;
        if (status.equals(CMD_ORDER_NOT_RECEIVE_AND_CMD_ORDER_IS_RECEIVE) || status.equals(CMD_ORDER_IS_VIEW)) {
            size = 0;
        }
        QueryOrderReq req = new QueryOrderReq.Builder().viewOrderId(order_id).status(statusMap.get(status)).minUpdateTime(minUpdateTime).maxUpdateTime(maxUpdateTime).pageSize(size).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_QueryOrder, req, status);
    }

    //管家接单
    public void stewardOrder(int order_id,String reqContext) {
        long uid = UserLogic.getInstance().getUid();
        StewardOrderReq req = new StewardOrderReq.Builder().orderStewardId(uid).viewOrderId(order_id).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_StewardOrder, req, reqContext);
    }

    //确认订房
    public void reserveRoom(int order_id, boolean isReserve, SignInfo signInfo, String remark) {
        ReserveRoomReq req = new ReserveRoomReq.Builder().viewOrderId(order_id).isReserve(isReserve).signInfo(signInfo).unSignRemark(remark).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_ReserveRoom, req, null);
    }

    //添加订房
    public void addReserveRoom(SignInfo signInfo) {
        AddReserveRoomReq req = new AddReserveRoomReq.Builder().signInfo(signInfo).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_AddReserveRoom, req, null);
    }

    //模糊搜索房间
    public void searchRoomInfo(RoomFilter filter) {
        RoomSerachReq req = new RoomSerachReq.Builder().roomFilter(filter).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_RoomSerach, req, null);
    }

    //确认看房
    public void confirmView(int order_id, boolean isView, long viewTime, List<RoomInfo> rooms, String remark) {
        long uid = UserLogic.getInstance().getUid();
        ConfirmViewReq req = new ConfirmViewReq.Builder().viewOrderId(order_id).isView(isView).viewTime(viewTime).viewStewardId(uid).viewRooms(rooms).unViewRemark(remark).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_ConfirmView, req, null);
    }

    //再看看
    public void reView(int order_id, boolean isView, long viewTime, List<RoomInfo> rooms, String remark) {
        long uid = UserLogic.getInstance().getUid();
        ConfirmViewReq req = new ConfirmViewReq.Builder().viewOrderId(order_id).isView(isView).viewTime(viewTime).viewStewardId(uid).viewRooms(rooms).unViewRemark(remark).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_ReView, req, null);
    }

    //
    public void addVieRoom(String name, String gender, String phone, String job, long viewTime, String remark, String checkInDays, List<RoomInfo> rooms) {
        AddViewReq req = new AddViewReq.Builder().name(name).gender(gender).phone(phone).job(job).viewTime(viewTime).viewStewardId(UserLogic.getInstance().getUid()).remark(remark).checkInDays(checkInDays).viewRooms(rooms).build();
        sendReq(UserLogic.getInstance().getUid(), PMS_LOGIC_AddView, req, null);
    }

    public List<Order> getListByStatus(int status) {
        List<Order> resultList = new ArrayList<>();
        List<Order> cache = orderStatusMap.get(status);
        if (cache != null) {
            resultList.addAll(cache);
        }
        return resultList;
    }

    public List<Order> getHistoryOrderListByStatus(int status, boolean isOrder) {
        List<Order> resultList = new ArrayList<>();
        List<Order> cache = orderStatusMap.get(status);
        if (cache != null) {
            resultList.addAll(cache);
        }
        return resultList;
    }


    public long[] getMinMaxUpDateTime(int status) {
        long[] result = new long[2];
        result[0] = 0;
        result[1] = 0;
        List<Order> resultList = orderStatusMap.get(status);
        if (resultList != null && resultList.size() >= 1) {
            if (resultList.size() == 1) {
                result[1] = resultList.get(0).viewOrder.updateTime;
                result[0] = resultList.get(0).viewOrder.updateTime;
            }
            if (resultList.size() > 1) {
                result[0] = resultList.get(resultList.size() - 1).viewOrder.updateTime;
                result[1] = resultList.get(0).viewOrder.updateTime;
            }
        }
        return result;
    }


    private List<Order> viewOrderToOrders(List<ViewOrder> viewOrders) {
        if (viewOrders != null && viewOrders.size() > 0) {
            List<Order> orders = new LinkedList<>();
            for (ViewOrder viewOrder : viewOrders) {
                Order newOrder = new Order(viewOrder);
                orders.add(newOrder);
            }
            return orders;
        } else {
            return new LinkedList<Order>();
        }
    }
    private void onRecieveViewOrder(ViewOrder viewOrder) {
        if (viewOrder != null) {
            Order order = new Order(viewOrder);
            List<Order> list = new ArrayList<>();
            list.add(order);
            onRecieveViewOrders(list,false);
        }
    }

    boolean isProcessingOrder(Order order) {
        if (order != null) {
            if (order.status == ViewOrderStatus.ORDER_NOT_RECEIVE.getValue() ||
                    order.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue() ||
                    order.status == ViewOrderStatus.ORDER_IS_VIEW.getValue()) {
                return true;
            }
        }
        return false;
    }


    /*
     * return updated orders
     */
    private List<Order> onRecieveViewOrders(List<Order> viewOrders,boolean fromDb) {
        List<Order> toSave = new LinkedList<>();
        Set<Integer> newOderStatus = new HashSet<>();
        List<Order> toRemove = new LinkedList<>();
        if (viewOrders != null && viewOrders.size() > 0) {
            for (Order order:viewOrders) {
                Order oldOrder = orderIdMap.get(order.id);
                if (order.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue() && order.viewOrder.orderStewardId != UserLogic.getInstance().getUid()) {
                    if (oldOrder != null) {
                        newOderStatus.add(oldOrder.status);
                        toRemove.add(oldOrder);
                    }
                    continue;
                }
                Order newOrder = null;
                if (oldOrder == null) {
                    newOrder = order;
                } else {
                    if (order.updateTime > oldOrder.updateTime) {
                        newOrder = order;
                    }
                }
                // adjust status map
                if (newOrder != null) {
                    if (oldOrder != null) {
                        newOderStatus.add(oldOrder.status);
                        toRemove.add(oldOrder);
                    }
                    toSave.add(newOrder);
                    newOderStatus.add(newOrder.status);
                }
            }
        }
        // adjust memory
        for (Order order : toRemove) {
            orderIdMap.remove(order.id);
            List<Order> orders = orderStatusMap.get(order.status);
            if (orders != null) {
                orders.remove(order);
            }
        }
        for (Order order : toSave) {
            orderIdMap.put(order.id, order);
            List<Order> orders = orderStatusMap.get(order.status);
            if (orders == null) {
                orders = new LinkedList<>();
                orderStatusMap.put(order.status,orders);
            }
            orders.add(order);
        }
        // sort
        for (Integer status : newOderStatus) {
            List<Order> list = orderStatusMap.get(status);
            if (list != null)
                Collections.sort(list,updateTimeComparator);
        }
        // adjust db
        if (!fromDb) {
           {
               for (Order order : toRemove) {
                   QLog.d(this,"onRecieveViewOrders removeOrder "+order.id+ " status= "+order.status+" ts="+order.updateTime);
                   if (isProcessingOrder(order)) {
                       getDb().deleteAll(ORMap.ORDER_PROCESSING,Order.class,"Id=?",String.valueOf(order.id));
                   } else {
                       getDb().deleteAll(ORMap.ORDER_HISTORY, Order.class, "Id=?", String.valueOf(order.id));

                   }
               }
                // save db
                List<Order> processes = new LinkedList<>();
                List<Order> history = new LinkedList<>();
                for (Order order : toSave) {
                    QLog.d(this,"onRecieveViewOrders saveOrder "+order.id+ " status= "+order.status+" ts="+order.updateTime);
                    if (isProcessingOrder(order)) {
                        processes.add(order);
                    } else {
                        history.add(order);
                    }
                }
                getDb().saveInTx(ORMap.ORDER_PROCESSING, processes);
                getDb().saveInTx(ORMap.ORDER_HISTORY, history);
            }
        }
        // notify ui
//        if (newOderStatus.contains(ViewOrderStatus.ORDER_NOT_RECEIVE.getValue()) ||
//                newOderStatus.contains(ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) ||
//                                newOderStatus.contains(ViewOrderStatus.ORDER_IS_VIEW.getValue()) ) {
//            UiObserverManager.getInstance().dispatchEvent(CMD_PROCESSING_ORDER_STATUS_UPDATE, true, null);
//        }
        UiObserverManager.getInstance().dispatchEvent(CMD_PROCESSING_ORDER_STATUS_UPDATE, true,null, null);

        return toSave;
    }

    public void removeOrder(final int orderId) {
        ThreadManager.getInstance().runOnWorkerThread(new Runnable() {
            @Override
            public void run() {
                if (orderIdMap.containsKey(orderId)) {
                    Order order = orderIdMap.remove(orderId);
                    QLog.d(this,"removeOrder "+orderId+" status= "+order.status+" ts="+order.updateTime);
                    List<Order> orderList = orderStatusMap.get(order.status);
                    if (orderList != null) {
                        orderList.remove(order);
                    }
                    if (isProcessingOrder(order)) {
                        getDb().deleteAll(ORMap.ORDER_PROCESSING,Order.class,"Id=?",String.valueOf(order.id));
                    } else {
                        getDb().deleteAll(ORMap.ORDER_HISTORY, Order.class, "Id=?", String.valueOf(order.id));
                    }
                    UiObserverManager.getInstance().dispatchEvent(CMD_PROCESSING_ORDER_STATUS_UPDATE, true,null, null);
                }
            }
        },0);
    }


    Comparator<Order> updateTimeComparator = new Comparator<Order>() {
        @Override
        public int compare(Order o1, Order o2) {
            Long time1 = o1.updateTime;
            Long time2 = o2.updateTime;
            return time2.compareTo(time1);
        }
    };


    private void onQueryOrderResp(Request req, Packet resp) {
        Boolean isFinish = false;
        String status = (String) req.obj;
        if (resp.ret == 0) {
            QueryOrderResp orderResp = getResp(resp, QueryOrderResp.class);
            QLog.d(this, "size:" + (orderResp.viewOrders != null ? orderResp.viewOrders.size() : 0) + " " + orderResp.toString());
            if (status.equals(CMD_ORDER_NOT_RECEIVE_AND_CMD_ORDER_IS_RECEIVE) ||
                    status.equals(CMD_ORDER_IS_VIEW)) {//未接单|| 已接单&&未联系
                getDb().deleteAll(ORMap.ORDER_PROCESSING, Order.class);
                // clear all 1,2,4
                if (status.equals(CMD_ORDER_NOT_RECEIVE_AND_CMD_ORDER_IS_RECEIVE)) {
                    orderStatusMap.remove(ViewOrderStatus.ORDER_NOT_RECEIVE.getValue());
                    orderStatusMap.remove(ViewOrderStatus.ORDER_IS_RECEIVE.getValue());
                    Set<Integer> orderIds = orderIdMap.keySet();
                    Set<Integer> orderIdsClone = new HashSet<>(orderIds);
                    for (Integer id : orderIdsClone) {
                        Order order = orderIdMap.get(id);
                        if (order.status == ViewOrderStatus.ORDER_NOT_RECEIVE.getValue() ||
                                order.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) {
                            orderIdMap.remove(id);
                        }
                    }
                    getDb().deleteAll(ORMap.ORDER_PROCESSING, Order.class,"Status=? or Status = ?","1","2");
                } else {
                    orderStatusMap.remove(ViewOrderStatus.ORDER_IS_VIEW.getValue());
                    Set<Integer> orderIds = orderIdMap.keySet();
                    Set<Integer> orderIdsClone = new HashSet<>(orderIds);
                    for (Integer id : orderIdsClone) {
                        Order order = orderIdMap.get(id);
                        if (order.status == ViewOrderStatus.ORDER_IS_VIEW.getValue()) {
                            orderIdMap.remove(id);
                        }
                    }
                    getDb().deleteAll(ORMap.ORDER_PROCESSING, Order.class, "Status=?", "4");
                }
            }
            List<ViewOrder> list = orderResp.viewOrders;
            //数据库缓存删除当前分类，并且存入当前分类
            onRecieveViewOrders(viewOrderToOrders(list), false);
            if (list == null || list.size() < pageSize) {
                isFinish = true;
            }
        }
        UiObserverManager.getInstance().dispatchEvent(status, resp.ret == 0,resp.error, new Object[]{isFinish});
    }


    private void onSerwardOrderResp(Request req, Packet resp) {
        ViewOrder returnOrder =null;
        if (resp.ret == 0) {
            StewardOrderResp stewardOrderResp = getResp(resp, StewardOrderResp.class);
            if (stewardOrderResp != null && stewardOrderResp.viewOrder != null) {
                returnOrder = stewardOrderResp.viewOrder;
                onRecieveViewOrder(stewardOrderResp.viewOrder);
            }
        } else if (resp.ret == AllRetCode.CODE_VIEW_ORDER_HAS_RECEIVE) {
            StewardOrderReq reqs = (StewardOrderReq) req.oriPb;
            Order order = orderIdMap.remove(reqs.viewOrderId);
            List<Order> orderList = orderStatusMap.get(order.status);
            if (orderList != null)
                orderList.remove(order);
            getDb().deleteAll(ORMap.ORDER_PROCESSING, Order.class, "Id=?", String.valueOf(order.id));
        }
        UiObserverManager.getInstance().dispatchEvent(req.packet.cmd, resp.ret == 0,resp.error, new Object[]{returnOrder,resp.ret,req.obj});
    }

    public void onPushOrder(List<Msg> msgList) {
        QLog.d(this,"onPushOrder "+msgList.size());
        if (msgList.size() == 0) {
            return;
        }else{
            NotificationLogic.getInstance().ntfPeedOrder(msgList.size());
        }
        List<Order> recvs = new LinkedList<>();
        for (Msg msg:msgList) {
            byte[] bytes = msg.msgBody.context.toByteArray();
            try {
                ViewOrder viewOrder = wire.parseFrom(bytes,ViewOrder.class);
                Order order = new Order(viewOrder);
                recvs.add(order);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        showNotify(onRecieveViewOrders(recvs, false),true);
    }

    private void showNotify(List<Order> orders,boolean showNotification) {
        if (orders != null) {
            for (Order order : orders) {
                if (order.status == ViewOrderStatus.ORDER_NOT_RECEIVE.getValue()) {
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(
                            App.getApp());
                    builder.setAutoCancel(true);
                    builder.setContentTitle("小螺管家");
                    builder.setContentText("有客户预约看房啦,快去联系");
                    builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
                    builder.setSmallIcon(R.mipmap.ic_launcher);
                    Intent intent = new Intent(App.getApp(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(App.getApp(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    builder.setContentIntent(pendingIntent);
                    NotificationManager mNotificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
                    mNotificationManager.notify(1, builder.build());
                    break;
                }
            }

            for (Order order : orders) {
                if (order.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) {
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(
                            App.getApp());
                    builder.setAutoCancel(true);
                    builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
                    builder.setSmallIcon(R.mipmap.ic_launcher);
                    builder.setContentTitle("小螺管家");
                    builder.setContentText("有一个预约单分配给你了,赶快去联系吧");
                    Intent intent = new Intent(App.getApp(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(App.getApp(), 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    builder.setContentIntent(pendingIntent);
                    NotificationManager mNotificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
                    mNotificationManager.notify(2, builder.build());
                    break;
                }
            }
        }
    }

    private void onConfirmViewResp(Request req, Packet resp) {
        if (resp.ret == 0) {
            ConfirmViewResp confirmViewResp = getResp(resp, ConfirmViewResp.class);
            QLog.d(this, "resp " + confirmViewResp);
            if (confirmViewResp != null && confirmViewResp.viewOrder != null) {
                onRecieveViewOrder(confirmViewResp.viewOrder);
            }
        }
        UiObserverManager.getInstance().dispatchEvent(req.packet.cmd, resp.ret == 0,resp.error, null);
    }

    private void onAddViewResp(Request req, Packet resp) {
        if (resp.ret == 0) {
            AddViewResp addViewResp = getResp(resp, AddViewResp.class);
            if (addViewResp != null && addViewResp.viewOrder != null) {
                onRecieveViewOrder(addViewResp.viewOrder);
            }
        }
        UiObserverManager.getInstance().dispatchEvent(req.packet.cmd, resp.ret == 0,resp.error, null);
    }

    private void onReserveRoomResp(Request req, Packet resp) {
        if (resp.ret == 0) {
            ReserveRoomResp reserveRoomResp = getResp(resp, ReserveRoomResp.class);
            if (reserveRoomResp != null && reserveRoomResp.viewOrder != null) {
               onRecieveViewOrder(reserveRoomResp.viewOrder);
            }
        }
        UiObserverManager.getInstance().dispatchEvent(req.packet.cmd, resp.ret == 0,resp.error, new Object[]{resp.ret});
    }

    private void onSearchRoomInfoResp(Request req, Packet resp) {
        List<RoomInfo> rooms=null;
        if (resp.ret == 0) {
            RoomSerachResp roomSerachResp = getResp(resp, RoomSerachResp.class);
            rooms = roomSerachResp.rooms;
        }
        UiObserverManager.getInstance().dispatchEvent(req.packet.cmd, resp.ret == 0,resp.error, new Object[]{rooms});
    }
    @Override
    public void onResp(Request req, Packet resp) {
        super.onResp(req, resp);
        Packet packet = req.packet;
        String cmd = packet.cmd;
        if (cmd.equals(PMS_LOGIC_QueryOrder)) {
            onQueryOrderResp(req, resp);
        } else if (cmd.equals(PMS_LOGIC_StewardOrder)) {
            onSerwardOrderResp(req, resp);
        } else if (cmd.equals(PMS_LOGIC_ReserveRoom)) {
            onReserveRoomResp(req, resp);
            //UiObserverManager.getInstance().dispatchEvent(cmd, resp.ret == 0, null);
        } else if (cmd.equals(PMS_LOGIC_RoomSerach)) {
           onSearchRoomInfoResp(req, resp);
        } else if (cmd.equals(PMS_LOGIC_ConfirmView)) {
            onConfirmViewResp(req, resp);
        } else if (cmd.equals(PMS_LOGIC_AddView)) {
            onAddViewResp(req, resp);
        } else if (cmd.equals(PMS_LOGIC_AddReserveRoom)) {
            if (resp.ret == 0) {
                AddReserveRoomResp addReserveRoomResp = getResp(resp, AddReserveRoomResp.class);
            }
            UiObserverManager.getInstance().dispatchEvent(cmd, resp.ret == 0,resp.error, null);
        } else if (cmd.equals(PMS_LOGIC_ReView)) {
            onConfirmViewResp(req, resp);
        }
    }


}
